﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using commfuncs.MAP;
using commfuncs;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
using System.Net;
using System.IO;
using micsun.Logic;
namespace tmsWeb.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MapTokenService
    {
        [OperationContract]
        public string GetTileSourceMode()
        {
            return micsunConfiguration.GetParameter("TileSourceMode") as string;
        }
        [OperationContract]
        public SortedList<string ,Content> GetContens()
        {
            ContentBL bl = new ContentBL();
            System.Data.IDataReader rd = bl.GetAllContents();
            SortedList<string, Content> ContentList = new SortedList<string, Content>();
            while (rd.Read())
            {
                var ct = new Content
                {
                    Id = rd["id"].ToString(),
                    SiteID = rd["SiteID"].ToString(),
                    Portion = rd["Portion"].ToString(),
                    SiteName = rd["SiteName"].ToString()
                    
                 };
                int ri = bl.getSiteStateBySiteID(ct.Id);
                ct.Message = Misc.GetSpTextById(ri, "StateID", "StateName", "sp_ssrstate");
                ct.State = bl.getSiteStateBySiteID(ct.SiteID);
                ct.ImageName = bl.getSiteImage(ct.State);
                    
               
                ContentList.Add(ct.Id, ct);
            }
            return ContentList;
        }
        [OperationContract]
        public string SaveImageCach(string url, string z,string x, string y)
        {
            try
            {
                HttpWebResponse hwr = (HttpWebResponse)HttpWebRequest.Create(url).GetResponse();
                Stream respStream = hwr.GetResponseStream();
                Image img = Image.FromStream(respStream);
                
                //char[] cbuffer = new char[256];
                //int byteRead = 0;
                //string strBuff = string.Empty;

                //System.IO.StreamReader respStreamReader = new StreamReader(respStream);
                //byteRead = respStreamReader.Read(cbuffer, 0, 256);
                //while (byteRead != 0)
                //{

                //    String strResp = new String(cbuffer, 0, byteRead);

                //    strBuff = strBuff + strResp;
                    
                //    byteRead = respStreamReader.Read(cbuffer, 0, 256);

                //}

                //respStream.Close();



                string zPath = Global.AppPath + @"ClientBin\MapCache\" + z + @"\";
                if (!Directory.Exists(zPath))
                {
                    Directory.CreateDirectory(zPath);
                }
                string fileName = zPath + string.Format("x{0}y{1}.png", x, y);
                img.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                //using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
                //{
                //    byte[] byteSave = Encoding.ASCII.GetBytes(strBuff);

                //    fileStream.Write(byteSave, 0, byteSave.Length);

                //    fileStream.Close();
                //}
                return fileName;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            
        }
    }

    
}
